public class ThreadDemo11 {
    public static void main(String[] args) throws InterruptedException {
        Thread t=new Thread() {
            @Override
            public void run() {
                //默认情况下 isInterrupted()值为 false
                while ( !Thread.currentThread().isInterrupted() ) {
                    //Thread.currentThread() 获取当前线程的实例
                    System.out.println("线程运行中。。。");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        //当线程正在sleep时触发 InterruptedException
                        //加个 break 后，结束循环
                        break;
                    }
                }
                System.out.println("线程结束");
            }
        };

        t.start();

        //主线程中 通过 t.interrupt() 修改标记位
        Thread.sleep(3000);
        t.interrupt();//将 Thread.currentThread().isInterrupted() 修改为 true

    }
}
